﻿#include <QIntValidator>
#include "DlgBase.h"
#include "MyVtkViewWindow.h"

//------------------- DlgBase ------------------//
DlgBase::DlgBase(QWidget* parent):
    QDialog(parent)
{
    //隐藏帮助图标
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
    //禁止最大化按钮
    setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);

    setBaseSize(800, 800);

    initUI();
    manageConnections();
}

DlgBase::~DlgBase()
{
}

void DlgBase::initUI()
{
    setWindowTitle("Base Dialog");
}

void DlgBase::manageConnections()
{
}




//------------------- IntEdit ------------------//
MyDoubleEdit::MyDoubleEdit(QWidget* parent, const QString& title,
                           double defaultValue, double minValue,
                           double maxValue, int decimals)
    :QWidget(parent)
{
    initUI();
    setTitle(title);
    setValue(defaultValue);
    setRange(minValue, maxValue);
    setDecimals(decimals);
    manageConnections();
}



MyDoubleEdit::~MyDoubleEdit()
{
}

void MyDoubleEdit::setValue(double value)
{
    m_value = value;
    m_lineEdit->setValue(value);
    m_lineEdit->update();
}

double MyDoubleEdit::getValue() const
{
    return m_value;
}

void MyDoubleEdit::setMinValue(double value)
{
    setRange(value, m_maxValue);
}

double MyDoubleEdit::getMinValue() const
{
    return m_minValue;
}

void MyDoubleEdit::setMaxValue(double value)
{
    setRange(m_minValue, value);
}

double MyDoubleEdit::getMaxValue() const
{
    return m_maxValue;
}

void MyDoubleEdit::setRange(double min, double max)
{
    m_minValue = min;
    m_maxValue = max;
    m_lineEdit->setRange(min, max);
    m_lineEdit->setValue(m_value);
}

void MyDoubleEdit::setDecimals(int decimals)
{
    m_lineEdit->setDecimals(decimals);
    m_lineEdit->setValue(m_value);
    m_value = m_lineEdit->value();
}

int MyDoubleEdit::getDecimals() const
{
    return m_lineEdit->decimals();
}

void MyDoubleEdit::setTitle(const QString& title)
{
    m_title = title;
    m_label->setText(title);
}

QString MyDoubleEdit::getTitle() const
{
    return QString();
}

void MyDoubleEdit::initUI()
{
    m_label = new QLabel(this);
    m_label->setMinimumWidth(100);

    m_lineEdit = new QDoubleSpinBox(this);
    m_lineEdit->setMinimumWidth(100);
    m_lineEdit->setKeyboardTracking(false); //QDoubleSpinBox取消滚动
    m_lineEdit->setMouseTracking(false); //QDoubleSpinBox取消滚动

    m_layout = new QHBoxLayout(this);
    m_layout->addWidget(m_label);
    m_layout->addWidget(m_lineEdit);
    setLayout(m_layout);
}

void MyDoubleEdit::manageConnections()
{
    connect(m_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(onValueChanged()));
}

void MyDoubleEdit::onValueChanged()
{
    m_value = m_lineEdit->value();
    emit valueChanged(m_value);
}


//------------------- CoordinateEdit ------------------//
MyCoordinateEdit::MyCoordinateEdit(QWidget* parent, const QString& title,
                                   double defaultValue[4], bool if4D, int decimals):
    QGroupBox(parent)
{
    initUI();
    setTitle(title);

    if(defaultValue)
    {
        setValues(defaultValue[0], defaultValue[1], defaultValue[2], defaultValue[3]);
    }

    setIf4D(if4D);
    setDecimals(decimals);
    manageConnections();
}

MyCoordinateEdit::~MyCoordinateEdit()
{
}

void MyCoordinateEdit::setIf4D(bool if4D)
{
    m_if4D = if4D;
    m_wEdit->setVisible(m_if4D);
}

void MyCoordinateEdit::setValues(double x, double y, double z, double w)
{
    m_xEdit->setValue(x);
    m_yEdit->setValue(y);
    m_zEdit->setValue(z);
    m_wEdit->setValue(w);
}

void MyCoordinateEdit::getValues(double& x, double& y, double& z, double& w) const
{
    x = m_xEdit->getValue();
    y = m_yEdit->getValue();
    z = m_zEdit->getValue();
    w = m_wEdit->getValue();
}

void MyCoordinateEdit::getValues(double& x, double& y, double& z) const
{
    x = m_xEdit->getValue();
    y = m_yEdit->getValue();
    z = m_zEdit->getValue();
}

void MyCoordinateEdit::setDecimals(int decimals)
{
    double x, y, z, w;
    getValues(x, y, z, w);//get original values

    m_xEdit->setDecimals(decimals);
    m_yEdit->setDecimals(decimals);
    m_zEdit->setDecimals(decimals);
    m_wEdit->setDecimals(decimals);

    setValues(x, y, z, w);//set new values
}


MyDoubleEdit* MyCoordinateEdit::getXEdit() const
{
    return m_xEdit;
}

MyDoubleEdit* MyCoordinateEdit::getYEdit() const
{
    return m_yEdit;
}

MyDoubleEdit* MyCoordinateEdit::getZEdit() const
{
    return m_zEdit;
}

MyDoubleEdit* MyCoordinateEdit::getWEdit() const
{
    return m_wEdit;
}

void MyCoordinateEdit::initUI()
{
    m_xEdit = new MyDoubleEdit(this, "X", 0.0, -1000000.0, 1000000.0);
    m_yEdit = new MyDoubleEdit(this, "Y", 0.0, -1000000.0, 1000000.0);
    m_zEdit = new MyDoubleEdit(this, "Z", 0.0, -1000000.0, 1000000.0);
    m_wEdit = new MyDoubleEdit(this, "W", 1.0, 0.0, 1000000.0);

    m_layout = new QVBoxLayout(this);
    m_layout->addWidget(m_xEdit);
    m_layout->addWidget(m_yEdit);
    m_layout->addWidget(m_zEdit);
    m_layout->addWidget(m_wEdit);
    setLayout(m_layout);
}

void MyCoordinateEdit::manageConnections()
{
    connect(m_xEdit, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double)));
    connect(m_yEdit, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double)));
    connect(m_zEdit, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double)));
    connect(m_wEdit, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double)));
}

void MyCoordinateEdit::onValueChanged(double value)
{
    emit valueChanged(value);
}
